package com.dream.common.utils;


import com.dream.dto.DataResponse;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ActionUtils {


    private static String getEnvStr(){
        return System.getProperty("spring.profiles.active");
    }

    /**
     * 是否是开发环境
     */
    private static Boolean isDevEnv = null;

    /**
     * 是否是开发环境
     */
    public static boolean isDevEnv() {
        if (ActionUtils.isDevEnv == null) {
            ActionUtils.isDevEnv = "dev".equals(getEnvStr());
        }
        return ActionUtils.isDevEnv;
    }

    /**
     * 是否是体验测试环境
     */
    private static Boolean isTestEnv = null;

    /**
     * 是否是测试环境
     */
    public static boolean isTestEnv() {
        if (ActionUtils.isTestEnv == null) {
            ActionUtils.isTestEnv = "test".equals(getEnvStr());
        }
        return ActionUtils.isTestEnv;
    }

    /**
     * 是否是正式环境
     */
    private static Boolean isIdcEnv = null;

    /**
     * 是否是正式环境
     */
    public static boolean isIdcEnv() {
        if (ActionUtils.isIdcEnv == null) {
            ActionUtils.isIdcEnv = "idc".equals(getEnvStr());
        }
        return ActionUtils.isIdcEnv;
    }

    /**
     * 输出打印流
     * @param response 响应对象
     * @param dataResponse 响应数据体
     */
    public static void printWriter(HttpServletResponse response,DataResponse dataResponse) {
        response.setContentType("text/html;charset=utf-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.write(JsonUtils.toJSONString(dataResponse));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                out.flush();
                out.close();
            }
        }
    }

}
